package com.fengwk.cv4j.view.common;

import com.fengwk.cv4j.compute.Compute;
import com.fengwk.cv4j.factory.ViewFactory;
import com.fengwk.cv4j.view.View;
import com.fengwk.cv4j.view.ViewImpl;
import com.fengwk.support.exception.BaseException;

/**
 * 通用的取模算子
 * 
 * @author fengwk
 *
 */
public class ModView<T> extends ViewImpl<T> {

    private static final long serialVersionUID = -8699621490802577374L;

    public ModView(Compute<T> compute, ViewFactory<T> factory, @SuppressWarnings("unchecked") View<T>...vs) {
        super(compute, factory, vs);
        checkVs(2, vs);
    }

    @Override
    protected T compute0() {
        return c().mod(t(0), t(1));
    }

    @Override
    protected View<T> gradient0(int idx) {
        if (idx == 0) 
            return f().of(1, t(1));
        if (idx == 1)
        	return f().neg(f().floor(f().div(v(0), v(1))));
        throw new BaseException("idx != 0 && idx != 1");
    }
    
    @Override
    public String toString() {
        return "mod";
    }
    
}
